package mycoffee.order.activity;

import mycoffee.order.utils.LogUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

public class BaseActivity extends Activity {
	/**
	 * A dialog showing a progress indicator and an optional text message or
	 * view.
	 */
	protected ProgressDialog mProgressDialog;
	/** the flag to know that this activity has been destroyed. */
	protected boolean mIsDestroy = false;
	/** the flag to know that this activity has been stopped. */
	protected boolean mIsStop = false;
	/** the flag to know that this activity has been paused. */
	protected boolean mIsPause = false;
	/**
	 * Showing a quick little message for the user. It's will be cancel when
	 * finish activity.
	 */
	private Toast mToast;

	private static final String TAG = "ActivityBase";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LogUtils.logInfo(TAG, this.getClass().toString() + " : onCreate");
		System.gc();
		System.runFinalization();
	}

	@Override
	protected void onResume() {
		super.onResume();

		LogUtils.logInfo(TAG, this.getClass().toString() + " : onResume");
		System.gc();
		System.runFinalization();
		mIsPause = false;
		mIsStop = false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		LogUtils.logInfo(TAG, this.getClass().toString() + " : onPause");

		dismissProgress();

		System.gc();
		System.runFinalization();

		mIsPause = true;
	}

	@Override
	protected void onStop() {
		mIsStop = true;
		if (mToast != null) {
			cancelToast();
		}
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mIsDestroy = true;
		mToast = null;
		mProgressDialog = null;
		LogUtils.logInfo(TAG, this.getClass().toString() + " : onDestroy");

		System.gc();
		System.runFinalization();
	}

	/**
	 * show progress dialog.
	 * 
	 * @param msgResId
	 */
	public void showProgress(int msgResId) {
		LogUtils.logError(TAG, "showProgress");
		showProgress(msgResId, null);
	}

	/**
	 * mIsDestroy = true;
	 * 
	 * @param msgResId
	 * @param keyListener
	 */
	public void showProgress(int msgResId,
			DialogInterface.OnKeyListener keyListener) {

		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			return;
		}
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setIndeterminate(true);
		if (msgResId != 0) {
			mProgressDialog.setMessage(getResources().getString(msgResId));
		}

		if (keyListener != null) {
			mProgressDialog.setOnKeyListener(keyListener);
		} else {
			mProgressDialog.setCancelable(false);
		}
		LogUtils.logError(TAG, this.getClass().toString() + " : showProgress");
		mProgressDialog.show();
	}

	/**
	 * cancel progress dialog.
	 */
	public void dismissProgress() {
		LogUtils.logInfo(TAG, this.getClass().toString() + " : dismissProgress");
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}

	/**
	 * Show message by the Toast object, it will be canceled when finish this
	 * activity.
	 * 
	 * @param msg
	 *            message wants to show
	 */
	public final void showToastMessage(final CharSequence msg) {
		if (mToast != null) {
			// Cancel last message toast
			if (mToast.getView().isShown()) {
				mToast.cancel();
			}
			mToast.setText(msg);
			mToast.show();
		}
	}

	/**
	 * Show message by the Toast object, it will be canceled when finish this
	 * activity.
	 * 
	 * @param resId
	 *            id of message wants to show
	 */
	public final void showToastMessage(final int resId) {
		if (mToast != null) {
			if (!mToast.getView().isShown()) {
				mToast.cancel();
			}
			mToast.setText(getText(resId));
			mToast.show();
		}
	}

	/**
	 * Cancel toast.
	 */
	public final void cancelToast() {
		if (mToast != null && mToast.getView().isShown()) {
			mToast.cancel();
		}
	}

	/**
	 * Show toast message, the message is not canceled when finish this
	 * activity.
	 * 
	 * @param msg
	 *            message wants to show
	 */
	public final void showToastMessageNotRelease(final CharSequence msg) {
		Toast toast = Toast.makeText(getApplicationContext(), "",
				Toast.LENGTH_LONG);
		if (toast != null) {
			// Cancel last message toast
			if (toast.getView().isShown()) {
				toast.cancel();
			}
			toast.setText(msg);
			toast.show();
		}
	}

	/**
	 * Show toast message, the message is not canceled when finish this
	 * activity.
	 * 
	 * 
	 * @param resId
	 *            id of message wants to show
	 */
	public final void showToastMessageNotRelease(final int resId) {
		Toast toast = Toast.makeText(getApplicationContext(), "",
				Toast.LENGTH_LONG);
		if (toast != null) {
			if (!toast.getView().isShown()) {
				toast.cancel();
			}
			toast.setText(getText(resId));
			toast.show();
		}
	}
}
